{% set pypi_name = 'keystone' %}
{% set source = fetch_source('https://tarballs.openstack.org/keystone/keystone-master.tar.gz') %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}
%global with_doc 1
%if 0%{?rhel} || 0%{?fedora}
%global rdo 1
%endif
Name:           {{ py2name() }}
Epoch:          {{ epoch('keystone') }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Summary:        OpenStack Identity Service
License:        {{ license('Apache-2.0') }}
URL:            https://docs.openstack.org/keystone
Source0:        {{ source|basename }}
Source1:        openstack-keystone.logrotate
Source2:        openstack-keystone.sysctl
Source3:        openstack-keystone.tmpfiles
Source4:        openstack-keystone.defaultconf
Source5:        openstack-keystone.README.config
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('Flask') }}
BuildRequires:  {{ py3('Flask-RESTful') }}
BuildRequires:  {{ py3('WebTest') }}
BuildRequires:  {{ py3('bcrypt') }}
BuildRequires:  {{ py3('fixtures') }}
BuildRequires:  {{ py3('freezegun') }}
BuildRequires:  {{ py3('jsonschema') }}
BuildRequires:  {{ py3('keystonemiddleware') }}
BuildRequires:  {{ py3('ldappool') }}
BuildRequires:  {{ py3('lxml') }}
BuildRequires:  {{ py3('mock') }}
BuildRequires:  {{ py3('oauthlib') }}
BuildRequires:  {{ py3('oslo.cache') }}
BuildRequires:  {{ py3('oslo.config') }}
BuildRequires:  {{ py3('oslo.db') }}
BuildRequires:  {{ py3('oslo.messaging') }}
BuildRequires:  {{ py3('oslo.policy') }}
BuildRequires:  {{ py3('oslo.upgradecheck') }}
BuildRequires:  {{ py3('oslotest') }}
BuildRequires:  {{ py3('osprofiler') }}
BuildRequires:  {{ py3('passlib') }}
BuildRequires:  {{ py3('pbr') }}
BuildRequires:  {{ py3('pysaml2') }}
BuildRequires:  {{ py3('requests') }}
BuildRequires:  {{ py3('scrypt') }}
BuildRequires:  {{ py3('sqlparse') }}
BuildRequires:  {{ py3('stestr') }}
BuildRequires:  {{ py3('testresources') }}
BuildRequires:  {{ py3('testscenarios') }}
BuildRequires:  {{ py3('testtools') }}
BuildRequires:  which
Requires:       logrotate
Requires:       python3-{{ pypi_name }} = %{epoch}:%{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  libxmlsec1-openssl1
Requires(pre):  pwdutils
%endif
%if 0%{?rdo}
BuildRequires:  xmlsec1-openssl
Requires(pre):  shadow-utils
%endif

%description
Keystone is a Python implementation of the OpenStack
(http://www.openstack.org) identity service API.
.
This package contains the keystone python libraries.

%package -n     python3-{{ pypi_name }}
Summary:        Keystone Python libraries
Group:          Applications/System
Requires:       {{ py3('python-ldap') }}
Requires:       {{ py3('Babel') }}
Requires:       {{ py3('Flask') }}
Requires:       {{ py3('Flask-RESTful') }}
Requires:       {{ py3('PyJWT') }}
Requires:       {{ py3('SQLAlchemy') }}
Requires:       {{ py3('WebOb') }}
Requires:       {{ py3('bcrypt') }}
Requires:       {{ py3('cryptography') }}
Requires:       {{ py3('dogpile.cache') }}
Requires:       {{ py3('jsonschema') }}
Requires:       {{ py3('python-keystoneclient') }}
Requires:       {{ py3('keystonemiddleware') }}
Requires:       {{ py3('ldappool') }}
Requires:       {{ py3('msgpack') }}
Requires:       {{ py3('oauthlib') }}
Requires:       {{ py3('oslo.cache') }}
Requires:       {{ py3('oslo.concurrency') }}
Requires:       {{ py3('oslo.config') }}
Requires:       {{ py3('oslo.context') }}
Requires:       {{ py3('oslo.db') }}
Requires:       {{ py3('oslo.i18n') }}
Requires:       {{ py3('oslo.log') }}
Requires:       {{ py3('oslo.messaging') }}
Requires:       {{ py3('oslo.middleware') }}
Requires:       {{ py3('oslo.policy') }}
Requires:       {{ py3('oslo.serialization') }}
Requires:       {{ py3('oslo.upgradecheck') }}
Requires:       {{ py3('oslo.utils') }}
Requires:       {{ py3('osprofiler') }}
Requires:       {{ py3('passlib') }}
Requires:       {{ py3('pbr') }}
Requires:       {{ py3('pycadf') }}
Requires:       {{ py3('pysaml2') }}
Requires:       {{ py3('python-memcached') }}
Requires:       {{ py3('pytz') }}
Requires:       {{ py3('scrypt') }}
Requires:       {{ py3('six') }}
Requires:       {{ py3('sqlalchemy-migrate') }}
Requires:       {{ py3('stevedore') }}

%description -n   python3-{{ pypi_name }}
Keystone is a Python implementation of the OpenStack
(https://docs.openstack.org/keystone/latest/) identity service API.
This package contains the Keystone Python library.

%package -n python3-{{ pypi_name }}-tests
Summary:        Keystone tests
Requires:       openstack-{{ pypi_name }} = %{epoch}:%{version}-%{release}
Provides:       python-keystone-tests = %{epoch}:%{version}-%{release}

%description -n python3-{{ pypi_name }}-tests
Keystone is a Python implementation of the OpenStack
(https://docs.openstack.org/keystone/latest/) identity service API.

This package contains the Keystone test files.

%if 0%{?with_doc}
%package doc
Summary:        Documentation for OpenStack Identity Service
Group:          Documentation/HTML
BuildRequires:  {{ py3('Sphinx') }}
BuildRequires:  {{ py3('openstackdocstheme') }}
BuildRequires:  {{ py3('sphinx-feature-classification') }}
BuildRequires:  {{ py3('sphinxcontrib-apidoc') }}
BuildRequires:  {{ py3('sphinxcontrib-blockdiag') }}
BuildRequires:  {{ py3('sphinxcontrib-seqdiag') }}

%description doc
OpenStack Keystone documentaion.
.
This package contains the documentation
%endif

%prep
%autosetup -p1 -n {{ pypi_name }}-{{upstream_version}}
%py_req_cleanup
%if 0%{?rdo}
find . \( -name .gitignore -o -name .placeholder \) -delete
find keystone -name \*.py -exec sed -i '/\/usr\/bin\/env python/d' {} \;
# adjust paths to WSGI scripts
sed -i 's#/local/bin#/bin#' httpd/wsgi-keystone.conf
sed -i 's#apache2#httpd#' httpd/wsgi-keystone.conf
%endif

%build
%{py3_build}

# doc
%if 0%{?with_doc}
PYTHONPATH=. PBR_VERSION={{ upstream_version }} %sphinx_build -b html doc/source doc/build/html
PYTHONPATH=. PBR_VERSION={{ upstream_version }} %sphinx_build -b man doc/source doc/build/man
# remove the Sphinx-build leftovers
rm -rf doc/build/html/.{doctrees,buildinfo}
rm -rf doc/build/man/.{doctrees,buildinfo}
%endif

# config file generation
PYTHONPATH=. oslo-config-generator --config-file config-generator/keystone.conf \
--output-file etc/keystone.conf.sample
# policy file generation
oslopolicy-sample-generator --config-file config-generator/keystone-policy-generator.conf --output-file etc/keystone.policy.yaml

%install
%{py3_install}
install -d -m 755 %{buildroot}%{_sysconfdir}/keystone
install -d -m 755 %{buildroot}%{_sysconfdir}/sysctl.d
install -d -m 750 %{buildroot}%{_localstatedir}/{lib,log,cache}/keystone
install -d -m 755 %{buildroot}%{_sysconfdir}/keystone/keystone.conf.d/
# default dir for fernet tokens
install -d -m 750 %{buildroot}%{_sysconfdir}/keystone/credential-keys/
install -D -m 644 %{SOURCE3} %{buildroot}/%{_tmpfilesdir}/keystone.conf
install -p -D -m 640 etc/keystone.conf.sample %{buildroot}%{_sysconfdir}/keystone/keystone.conf
install -D -m 640 %{SOURCE4} %{buildroot}/%{_sysconfdir}/keystone/keystone.conf.d/010-keystone.conf
install -D -m 440 %{SOURCE5} %{buildroot}/%{_sysconfdir}/keystone/README.config
install -p -D -m 640 etc/logging.conf.sample %{buildroot}%{_sysconfdir}/keystone/logging.conf
install -p -D -m 640 etc/keystone.policy.yaml %{buildroot}%{_sysconfdir}/keystone/keystone.policy.yaml
install -p -D -m 640 etc/default_catalog.templates %{buildroot}%{_sysconfdir}/keystone/default_catalog.templates
install -p -D -m 640 etc/sso_callback_template.html %{buildroot}%{_sysconfdir}/keystone/sso_callback_template.html
install -p -D -m 644 %{SOURCE1} %{buildroot}%{_sysconfdir}/logrotate.d/openstack-keystone
install -p -D -m 644 %{SOURCE2} %{buildroot}%{_sysconfdir}/sysctl.d/openstack-keystone.conf
# remove wrongly installed tree
rm -rv  %{buildroot}/%{_prefix}%{_sysconfdir}/
# Install sample data script.
install -p -D -m 755 tools/sample_data.sh %{buildroot}%{_datadir}/keystone/sample_data.sh
# Install apache configuration files
install -p -D -m 644 httpd/wsgi-keystone.conf  %{buildroot}%{_datadir}/keystone/

# man pages
%if 0%{?with_doc}
mkdir -p %{buildroot}%{_mandir}/man1
install -p -D -m 644 doc/build/man/*.1 %{buildroot}%{_mandir}/man1/
%endif

%pre
%openstack_pre_user_group_create keystone keystone /sbin/nologin
exit 0

%check
# don't want to depend on hacking for package building
rm keystone/tests/unit/test_hacking_checks.py
%if 0%{?suse_version}
PYTHON=python3 stestr run --black-regex keystone.tests.unit.test_policy.GeneratePolicyFileTestCase.test_policy_generator_from_command_line
%endif
%if 0%{?rdo}
stestr-3 run --black-regex keystone.tests.unit.test_policy.GeneratePolicyFileTestCase.test_policy_generator_from_command_line
%endif

%post
%tmpfiles_create %{_tmpfilesdir}/keystone.conf
%sysctl_apply openstack-keystone.conf

%files
%license LICENSE
%doc README.rst
%{_bindir}/keystone-wsgi-admin
%{_bindir}/keystone-wsgi-public
%{_bindir}/keystone-manage
%{_bindir}/keystone-status
%_tmpfilesdir/keystone.conf
%dir %{_datadir}/keystone
%attr(0755, root, root) %{_datadir}/keystone/sample_data.sh
%attr(0644, root, keystone) %{_datadir}/keystone/wsgi-keystone.conf
%dir %attr(0750, root, keystone) %{_sysconfdir}/keystone/
%dir %attr(0750, root, keystone) %{_sysconfdir}/keystone/keystone.conf.d/
%dir %attr(0750, root, keystone) %{_sysconfdir}/keystone/credential-keys
%{_sysconfdir}/keystone/README.config
%config(noreplace) %attr(0640, root, keystone) %{_sysconfdir}/keystone/keystone.conf
%config(noreplace) %attr(0640, root, keystone) %{_sysconfdir}/keystone/keystone.conf.d/010-keystone.conf
%config(noreplace) %attr(0640, root, keystone) %{_sysconfdir}/keystone/logging.conf
%config(noreplace) %attr(0640, root, keystone) %{_sysconfdir}/keystone/default_catalog.templates
%config(noreplace) %attr(0640, keystone, keystone) %{_sysconfdir}/keystone/keystone.policy.yaml
%config(noreplace) %attr(0640, keystone, keystone) %{_sysconfdir}/keystone/sso_callback_template.html
%config(noreplace) %{_sysconfdir}/logrotate.d/openstack-keystone
%dir %attr(0750, keystone, keystone) %{_localstatedir}/lib/keystone
%dir %attr(0750, keystone, keystone) %{_localstatedir}/log/keystone
%dir %attr(0750, keystone, keystone) %{_localstatedir}/cache/keystone
%{_sysconfdir}/sysctl.d/openstack-keystone.conf
%if 0%{?with_doc}
%{_mandir}/man1/keystone-manage.1.gz
%endif

%files -n python3-keystone
%doc README.rst
%license LICENSE
%{python3_sitelib}/keystone*
%{python3_sitelib}/keystone-*.egg-info
%exclude %{python3_sitelib}/{{ pypi_name }}/tests

%files -n python3-{{ pypi_name }}-tests
%license LICENSE
%{python3_sitelib}/{{ pypi_name }}/tests

%if 0%{?with_doc}
%files doc
%license LICENSE
%doc doc/build/html
%endif

%changelog
